/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.ubinow.mflex.baseui;

import net.ubinow.mflex.events.AbsEventPublisher;
import net.ubinow.mflex.events.Event;
import net.ubinow.mflex.util.Constants;
import net.ubinow.mflex.util.MiscUtil;


/**
 *
 * @author Josh
 */
public class SimpleTransition extends AbsEventPublisher implements ITransistion {

    private int _currentState = STATE_HIDING;
    private int _transitionInType = TRANSITION_NONE;
    private int _transitionOutType = TRANSITION_NONE;
    
    private int _screenWidth = Constants.MISC_SCREEN_WIDTH;
    private int _screenHeight = Constants.MISC_SCREEN_HEIGHT;
    
    private int _orgPositions[] = null;
    private int _positions[] = null;
    private IWidget[] _widgets = null;
    
    private MiscUtil _miscUtils = null;
    
    public SimpleTransition(){
        super();
        
        _miscUtils = MiscUtil.getInstance();
    }
    
    public SimpleTransition( int screenWidth, int screenHeight ){
        this();
        _screenWidth = screenWidth;
        _screenHeight = screenHeight;
    }
    
    public String getID() {
        return "SimpleTransition";
    }       

    public void prepareTransIn(IWidget[] widgets) {
        if( widgets == null )
            return; 
        
        _widgets = widgets;
        _positions = new int[widgets.length];
        
        // store actual positions and reasign transition methods 
        int len = widgets.length-1;
        for( int i=len; i>=0; i-- ){ 
            IWidget widget = widgets[i]; 
            
            if( widget == null )
                continue; 
            
            if( _transitionInType == TRANSITION_FLY_LEFT ){
                _positions[i] = widget.getOrgX();
                widget.setX( 0-( widget.getOrgX()+widget.getWidth() ) );
            }
            else if( _transitionInType == TRANSITION_FLY_RIGHT ){
                _positions[i] = widget.getOrgX();
                widget.setX( widget.getOrgX()+( _screenWidth-widget.getOrgX() ) );                
            }
            else if( _transitionInType == TRANSITION_FLY_DOWN ){
                System.out.println(" _transitionInType == TRANSITION_FLY_DOWN  - Not supported yet");
            }
            else{
                System.out.println("Not supported yet");
            }
        }
        
        // set the state 
        setState( STATE_TRANSITIONING_IN );
        
        
    }

    public void move() {        
        if( _currentState != STATE_TRANSITIONING_IN && _currentState != STATE_TRANSITIONING_OUT )
            return;
        
        if( _currentState == STATE_TRANSITIONING_IN ){
            System.out.println("TRANSITIONING IN");
        }
        else{
            System.out.println("TRANSITIONING OUT");
        }
        
        try{
        int len = _widgets.length-1;
        
        int countOfSetPositions = 0;
        for( int i=len; i>=0; i-- ){                       
            
            IWidget widget = _widgets[i];
            
            if( widget == null ){
                len--;
                continue; 
            }
            
//            System.out.println("Moving component; " + widget.getID() );
            
            if( _transitionInType == TRANSITION_FLY_LEFT || _transitionInType == TRANSITION_FLY_RIGHT 
                    || _transitionOutType == TRANSITION_FLY_LEFT || _transitionOutType == TRANSITION_FLY_RIGHT ){
                
                int p = _miscUtils.calcHalfDistance( _positions[i], widget.getX() );; 

                widget.setX( widget.getX()+p  );

                if( _currentState == STATE_TRANSITIONING_IN ){
//                    System.out.println("Transitioning IN: for widget " + widget.getID() + " - current x = " +
//                            widget.getX() + ", new position = " + _positions[i] );
                }else{                
                    System.out.println("Transitioning OUT: for widget " + widget.getID() + " - current x = " +
                            widget.getX() + ", new position = " + _positions[i] );
                }
                
                if( widget.getX() >= _positions[i]-GIVE_OR_TAKE && widget.getX() <= _positions[i]+GIVE_OR_TAKE ){
                    System.out.println("current position " + widget.getX() + ", should be " + _positions[i]);
                    widget.setX( _positions[i] );
                    countOfSetPositions++;
                }
            }
            else{
                
            }
        }
        
        if( countOfSetPositions >= len ){
            if( _currentState == STATE_TRANSITIONING_IN )
                setState( STATE_SHOWING );
            else
                setState( STATE_HIDING );
        }
        }
        catch( Exception e ){
            System.out.println("SimpleTransition.move; exception " + e.toString() );
            e.printStackTrace();
        }
    }
    
    private static final int GIVE_OR_TAKE = 10;

    public void prepareTransOut(IWidget[] widgets) {
        if( widgets == null )
            return; 
        
        _widgets = widgets;
        _positions = new int[widgets.length];
        _orgPositions = new int[widgets.length];
        
        // store actual positions and reasign transition methods 
        int len = _widgets.length-1;
        for( int i=len; i>=0; i-- ){ 
            IWidget widget = widgets[i];
            
            if( widget == null )
                continue; 
            
            // reemmber the actual position of the widget 
            _orgPositions[i] = widget.getX();
            
            if( _transitionOutType == TRANSITION_FLY_LEFT ){
                _positions[i] = -(widget.getOrgX()+widget.getWidth()); 
            }
            else if( _transitionOutType == TRANSITION_FLY_RIGHT ){
                _positions[i] = widget.getOrgX()+( _screenWidth-widget.getX() );                
            }
            else if( _transitionOutType == TRANSITION_FLY_DOWN ){
                System.out.println("Not supported yet - _transitionOutType == TRANSITION_FLY_DOWN");
            }
            else{
                System.out.println("Not supported yet");
            }
        }
        
        // set the state 
        setState( STATE_TRANSITIONING_OUT );
    }

    public void setState(int state) {
        _currentState = state; 
        
        if( _currentState == STATE_SHOWING ){
            notifySubscribers( getEvet( Event.EVT_TRANSITION_IN_FINISHED ) );
        }
        else if( _currentState == STATE_HIDING ){
            //resetWidgetPositions(); 
        	notifySubscribers( getEvet( Event.EVT_TRANSITION_OUT_FINISHED ) );
        }
    }
    /**
     * set the widgets positions to their original positions (using their original values 
     * stored in the orgPositions array)
     */
    private void resetWidgetPositions(){
        System.out.println("resetWidgetPositions");
        int len = _widgets.length-1;
        for( int i=len; i>=0; i-- ){ 
            IWidget widget = _widgets[i];
            
            if( widget == null )
                continue; 
            
            widget.setX( _orgPositions[i] );
        }
    }

    public int getState() {
        return _currentState;
    }

    public void setTransitionInType(int type) {
        _transitionInType = type; 
    }

    public int getTransitionInType() {
        return _transitionInType;
    }

    public void setTransitionOutType(int type) {
        _transitionOutType = type; 
    }

    public int getTransitionOutType() {
        return _transitionOutType;
    }

}
